
A Hashtable is a collection of keys, value pairs. We can use any type of value of key, also we can use any type of value. Hashtable can contain the dictionary object by the hashtable parameter. In the hashtable key can’t be null but value can. When we initialize the value into hashtable then we use the predefine “Add” method. We can also use many types of method into hashtable. Now given bellow the example code in hashtable and explain the code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using testForClass1;
namespace testFor
{
public class Program
{
static void Main(string[] args)
{
Hashtable myHashtable = new Hashtable();
myHashtable.Add(1, "Jesy");
myHashtable.Add(2, "Reza");
myHashtable.Add(3, "Tofail");
for (int i = 1; i <= myHashtable.Count; i++)
{
string name=(string)myHashtable[i];
Console.WriteLine(name);
}
// myHashtable.Add("Jesy", "Tofail");
Console.Read();
}
}
}
Hashtable work like dictionary but in the dictionary we must and should declare the key and value type but in hashtable we no need declare. First, we create an object in hashtable name as myHashtable then initialize the value using add method. Now we use the for-loop for print the value.